package it.hive.mediaplayer.beans.impl;

import it.hive.mediaplayer.beans.interfaces.RemoteMedia;

import java.net.URL;

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.image.Image;

public class Traccia implements RemoteMedia {
	private URL url;
	private Image image;
	private String nomeTraccia;
	// private SimpleStringProperty trackURL;
	private final SimpleStringProperty trackName;
	private boolean unparsed = false;

	public boolean isUnparsed() {
		return unparsed;
	}

	private String unparsedURL = null;

	public Traccia(URL url, Image image, String trackName) {
		// this.trackURL = new SimpleStringProperty(url.toString());
		this.trackName = new SimpleStringProperty(trackName);
		this.nomeTraccia = trackName;
		this.image = image;
		this.url = url;
	}

	public Traccia(String trackName, String unparsedURL) {
		unparsed = true;
		this.unparsedURL = unparsedURL;
		this.trackName = new SimpleStringProperty(trackName);
		this.nomeTraccia = trackName;
	}

	public String getUnparsedURL() {
		return unparsedURL;
	}

	public void setParsedURL(URL url) {
		if (unparsed) {
			unparsed = false;
			unparsedURL = null;
			// this.trackURL = new SimpleStringProperty(url.toString());
			this.url = url;
		}
	}

	/**
	 * @return the url
	 */
	public URL getUrl() {
		return url;
	}

	/**
	 * @return the image
	 */
	public Image getImage() {
		return image;
	}

	/**
	 * @param image
	 *            the image to set
	 */
	public void setImage(Image image) {
		this.image = image;
	}

	/**
	 * @return the annotation
	 */
	public String getNomeTraccia() {
		return nomeTraccia;
	}

	/**
	 * @param annotation
	 *            the annotation to set
	 */
	public void setNomeTraccia(String annotation) {
		this.nomeTraccia = annotation;
	}

	public String getTrackURL() {
		if (unparsed) {
			return null;
		}
		return url.toString(); // trackURL.get();
	}

	public String getTrackName() {
		return trackName.get();
	}

	@Override
	public boolean equals(Object arg0) {
		if (arg0 == null) {
			return false;
		}
		if (!(arg0 instanceof Traccia)) {
			return false;
		}
		Traccia object = (Traccia) arg0;
		if (object.isUnparsed() && this.unparsed) {
			return (object.getUnparsedURL().hashCode() == this.unparsedURL
					.hashCode());
		}
		if ((object.getUrl() == null && this.url != null)
				|| (object.getUrl() != null && this.url == null)) {
			return false;
		}
		if (object.getUrl() == null && this.url == null) {
			if (object.getNomeTraccia() != null && this.nomeTraccia != null) {
				return object.getNomeTraccia().hashCode() == this.nomeTraccia
						.hashCode();
			}
		}
		return (object.getUrl().hashCode() == this.url.hashCode());
	}

	public URL objectUrl() {
		return url;
	}

	public Image getImmagine() {
		return image;
	}

	@Override
	public String toString() {
		return nomeTraccia;
	}

}
